﻿// This file is part of Code4Public (http://code4public.codeplex.com)
// Copyright 2011 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
// ------------------------------------------------------------------

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using Code4Public.Utils;
using Code4Public.Parsing;
using Code4Public.GUI;

namespace Code4Public
{
    public partial class Form1 : Form
    {
        private readonly Core m_core = new Core();
        private readonly SyntaxHighlighter m_syntaxHighlighter = null;

        public Form1()
        {
            InitializeComponent();

            m_syntaxHighlighter = new SyntaxHighlighter(this.rtbInput, m_core);

            comboLanguages.Items.AddRange(m_core.Languages.LanguageNames.ToArray());

            if (comboLanguages.Items.Count > 0)
                comboLanguages.SelectedIndex = 0;

            try
            {
                m_core.Languages.SelectedLanguageName = comboLanguages.Text;
            }
            catch(ArgumentException)
            {
                MessageBox.Show("Selected Language does not exist");
            }

            comboColorProfiles.Items.AddRange(m_core.ColorProfiles.ProfileNames.ToArray());

            if (comboColorProfiles.Items.Count > 0)
                comboColorProfiles.SelectedIndex = 0;

            try
            {
                m_core.ColorProfiles.SelectedProfileName = comboColorProfiles.Text;
            }
            catch(ArgumentException)
            {
                MessageBox.Show("Selected Color Profile does not exist");
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            GC.Collect();

            /*
            rtbInput.Select(2, 1);
            rtbInput.SelectionColor = Color.Red;
            rtbInput.DeselectAll();

            rtbOutput.Text = rtbInput.Rtf;
            */
            /*
            if (!core.Languages.SelectLanguage(this.comboLanguages.Text))
            {
                MessageBox.Show("No such language!");
            }
            else
            {
                LanguageInfo selectedLanguage = languages.SelectedLanguage;
                //this.rtbOutput.Text = selectedLanguage.ToString();

                ParserHelper helper = new ParserHelper(selectedLanguage);

                StringBuilder sb = new StringBuilder();

                //foreach (var exp in helper.GetExpressions(rtbInput.Text))
                //{
                //    sb.AppendLine(exp.ToString());
                //}

                //rtbOutput.Text = sb.ToString();


                sb = new StringBuilder();
                sb.AppendLine("<pre>");

                foreach (var exp in helper.GetExpressions(rtbInput.Text))
                {
                    if (exp.Type == ExpressionType.Newline)
                        sb.AppendLine();
                    else
                        sb.Append(GetExpressionSpan(exp, expColors[exp.Type]));
                }

                sb.AppendLine("</pre>");

                //rtbOutput.Text = sb.ToString();
                webBrowser1.DocumentText = sb.ToString();
            } */
        }

        private string GetExpressionSpan(Expression exp, Color color)
        {
            var elem = new XElement("span",
                new XAttribute("style", "color:#" + ColorUtils.ColorTo6CharString(color)),
                exp.Content);

            return elem.ToString(SaveOptions.DisableFormatting);
        }

        private void comboLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_core.Languages.SelectedLanguageName = comboLanguages.Text;
                m_syntaxHighlighter.ReHighlight();
            }
            catch(ArgumentException)
            {
                MessageBox.Show("Selected Language does not exist");
            }
        }

        private void comboColorProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_core.ColorProfiles.SelectedProfileName = comboColorProfiles.Text;
                m_syntaxHighlighter.ReHighlight();
            }
            catch(ArgumentException)
            {
                MessageBox.Show("Selected color profile does not exist.");
            }
        }

        private void cbDisableHighlighting_CheckedChanged(object sender, EventArgs e)
        {
            m_syntaxHighlighter.DisableHighlighting = cbDisableHighlighting.Checked;
        }

        private void rtbInput_TextChanged(object sender, EventArgs e)
        {
            rtbOutput.Text = rtbInput.Rtf;
        }


    }
}
